
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO
Imports _2bMatesSite.Common


Namespace _2bMatesSite
	''' <summary>
	''' Summary description for upload_photo.
	''' </summary>
	Public Partial Class upload_photo
		Inherits BaseWebPage
		Protected PicturesTable As System.Web.UI.WebControls.Table
		Protected lblForbiddenUpload As System.Web.UI.WebControls.Label

		Protected Const IMAGES_ON_ROW As Integer = 3
		Protected Const MAX_FILE_SIZE As Integer = 512000
		' in bytes
		Protected Const MAX_TOTAL_FILE_SIZE As Integer = 7340032
		' in bytes
		Protected heightString As String = ""
		Protected htPageConstants As New Hashtable()

		Public Function CreateDataSource() As ICollection
			Dim usr As BO.User = LoggedUser
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			Dim pics As ArrayList = usr.GetPictures()

			Dim picsCount As Integer = pics.Count
			If picsCount = 0 Then
				lblNoImage.Visible = True
			Else
				lblNoImage.Visible = False
			End If

			Dim dt As New DataTable()
			Dim dr As DataRow

			dt.Columns.Add(New DataColumn("ImageURL", GetType(String)))
			dt.Columns.Add(New DataColumn("ImageID", GetType(String)))
			dt.Columns.Add(New DataColumn("Title", GetType(String)))
			dt.Columns.Add(New DataColumn("isDefault", GetType(Boolean)))
			dt.Columns.Add(New DataColumn("AddEditDescriptionText", GetType(String)))
			dt.Columns.Add(New DataColumn("Description", GetType(String)))
			dt.Columns.Add(New DataColumn("ViewLargerURL", GetType(String)))
			dt.Columns.Add(New DataColumn("Comments", GetType(String)))
			dt.Columns.Add(New DataColumn("FullName", GetType(String)))

			Dim i As Integer = 0
			While i < picsCount
				dr = dt.NewRow()

				Dim temp As UserPicture = DirectCast(pics(i), UserPicture)
				dr(0) = Pictures.GetPictureSrcFullURL(temp, True, "../")
				dr(1) = temp.ID.ToString()
				dr(2) = "Enter title here"
				dr(3) = (temp.ID = usr.DefaultPictureID)
				If temp.Desc = "" OrElse temp.Desc = Nothing Then
					dr(4) = "Add"
					dr(5) = ""
				Else
					dr(4) = "Edit"
					dr(5) = temp.Desc
				End If
				dr(6) = "../" + Constants.VIEW_PHOTO_ALBUM_COMMENTS_PAGE + "?img=" + temp.ID + "&usr=" + usr.ID
				If temp.NewComments.Count > 0 Then
					dr(7) = "View Comments(<font class='notes_text'>" + (temp.Comments.Count + temp.NewComments.Count) + "</font>)"
				Else
					dr(7) = "View Comments(" + (temp.Comments.Count + temp.NewComments.Count) + ")"
				End If
				dr(8) = usr.Name
				dt.Rows.Add(dr)
				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
			End While

			Dim dv As New DataView(dt)
			Return dv
		End Function

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here

			' An example displaying the first picture
			' this.pageTemplate.PageTitle = PageTitles.UPLOAD_PHOTO_PAGE;
			Dim usr As User = LoggedUser
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			Me.initPageTitles(Constants.UPLOAD_PHOTO, "", uploadImageForm, Nothing, htPageConstants)
			Me.initAdvertisements(uploadImageForm, Nothing)

			Dim totalPicturesLength As Integer = usr.GetPicturesTotalLength()

			'			if ( totalPicturesLength >= MAX_TOTAL_FILE_SIZE )
			'			{
			'				fldFileUpload.Disabled = true;
			'				lblForbiddenUpload.Text = "You have are not allowed to upload more than 500K total image size.";
			'				lblForbiddenUpload.Visible = true;
			'			}

			Dim regParam As String = Request.Params(Constants.REGISTRATION_PARAM)
			litSpacer.Text = "<IMG height='8' alt='' src='images/spacer.gif' width=1 border=0 >"

			If regParam <> Nothing AndAlso regParam <> "" AndAlso Convert.ToInt32(regParam) = Constants.REGISTRATION_VALUE Then
				btnSkip.Visible = True
				' this.pageTemplate.PageTitle = PageTitles.JOIN_PAGE;
				If usr.GetPictures().Count = 0 Then
					tblPhotos.Visible = False
					litSpacer.Visible = True
				Else
					btnSkip.Text = Me.GetPageConstText(htPageConstants, "CONST_Next_TEXT")
				End If
				heightString = "height='100%'"
				litSpacer.Visible = False
			End If

			If Not IsPostBack Then
				BindList()
			End If
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.dlPictures.ItemCommand, AddressOf Me.dlPictures_ItemCommand

		End Sub
#End Region

		Public Sub BindList()
			dlPictures.DataSource = CreateDataSource()
			dlPictures.DataBind()

			Dim pics As ArrayList = LoggedUser.GetPictures()
			Dim picsCount As Integer = pics.Count
			Dim btnDefaultTemp As Button
			Dim lblTemp As Label
			Dim btnTemp As HyperLink
			Dim i As Integer = 0
			While i < picsCount
				btnTemp = DirectCast(dlPictures.Items(i).FindControl("imgPhoto"), HyperLink)
				btnDefaultTemp = DirectCast(dlPictures.Items(i).FindControl("btnSetDefault"), Button)
				lblTemp = DirectCast(dlPictures.Items(i).FindControl("lblDefaultImage"), Label)
				If btnTemp.Attributes("isDefault").Equals("True") Then
					btnDefaultTemp.Visible = False
					lblTemp.Visible = True
				Else
					btnDefaultTemp.Visible = True
					lblTemp.Visible = False
				End If
				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
			End While
		End Sub

		Public Sub dlPictures_ItemCommand(ByVal Sender As Object, ByVal e As DataListCommandEventArgs)
			Dim src As Object = e.CommandSource
			Dim cmd As String = ""
			Dim usr As BO.User = LoggedUser
			Dim type As String = src.[GetType]().ToString()
			Select Case type
				Case "System.Web.UI.WebControls.ImageButton"
					cmd = (DirectCast(src, ImageButton)).CommandName
					Exit Select
				Case "System.Web.UI.WebControls.LinkButton"
					cmd = (DirectCast(src, LinkButton)).CommandName
					Exit Select
				Case "System.Web.UI.WebControls.Button"
					cmd = (DirectCast(src, Button)).CommandName
					Exit Select
			End Select
			Select Case cmd
				Case "SetDefault"
					usr.DefaultPictureID = Convert.ToInt32(e.CommandArgument)
					usr.Save()
					Exit Select
				Case "ViewLargerPhotoLink"
					Response.Redirect(Constants.VIEW_PHOTO_ALBUM_COMMENTS_PAGE + "?img=" + e.CommandArgument.ToString() + "&usr=" + LoggedUser.ID)
					Exit Select
				Case "Remove"
					Response.Redirect(Constants.DELETE_PHOTO + "?id=" + e.CommandArgument.ToString())
					Exit Select
				Case "ViewComments"
					Response.Redirect(Constants.VIEW_PHOTO_ALBUM_COMMENTS_PAGE + "?img=" + e.CommandArgument + "&usr=" + LoggedUser.ID)
					Exit Select
				Case "AddDescription"
					Response.Redirect(Constants.ADD_PHOTO_DESCRIPTION + "?id=" + e.CommandArgument)
					Exit Select
			End Select
			BindList()
		End Sub

		Protected Sub validateFile(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
			Dim usr As User = Me.LoggedUser

			Dim fileName As String = args.Value.Trim()
			Dim fileNameInLowerCase As String = fileName.ToLower()
			Dim fileSize As Integer = fldFileUpload.PostedFile.ContentLength
			Dim totalPicturesLength As Integer = usr.GetPicturesTotalLength()

			If fileNameInLowerCase.EndsWith(".gif") OrElse fileNameInLowerCase.EndsWith(".jpg") OrElse fileNameInLowerCase.EndsWith(".png") Then
				If fileSize > MAX_FILE_SIZE Then
					valFileFormat.ErrorMessage = Me.GetPageConstText(htPageConstants, "CONST_MsgErrMaxFileSize_TEXT")
					args.IsValid = False
				Else
					If (totalPicturesLength + fileSize) > MAX_TOTAL_FILE_SIZE Then
						valFileFormat.ErrorMessage = Me.GetPageConstText(htPageConstants, "CONST_MsgErrFileSizeLimit_TEXT")
						args.IsValid = False
					Else
						args.IsValid = True
					End If
				End If
			Else
				args.IsValid = False
			End If
		End Sub

		Protected Sub UploadImage(ByVal file As HttpPostedFile)
			Dim splitFileName As String() = file.FileName.Split(New Char() {"/"C, "\"C})

			LoggedUser.AddPicture(file.InputStream, splitFileName(splitFileName.Length - 1), "")
		End Sub

		Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			If Page.IsValid Then
				Dim usr As BO.User = LoggedUser
				UploadImage(fldFileUpload.PostedFile)
				If usr.DefaultPicture = Nothing Then
					Dim pics As ArrayList = usr.GetPictures()
					usr.DefaultPictureID = (DirectCast(pics(0), UserPicture)).ID
					usr.Save()
				End If
				tblPhotos.Visible = True
				Dim regParam As String = Request.Params(Constants.REGISTRATION_PARAM)
				If regParam <> Nothing AndAlso regParam <> "" AndAlso Convert.ToInt32(regParam) = Constants.REGISTRATION_VALUE Then
					btnSkip.Visible = True
					btnSkip.Text = Me.GetPageConstText(htPageConstants, "CONST_Next_TEXT")
					Response.Redirect(Constants.UPLOAD_PHOTO + "?" + Constants.REGISTRATION_PARAM + "=" + Constants.REGISTRATION_VALUE)
				Else
					Response.Redirect(Constants.UPLOAD_PHOTO)
				End If
			End If
		End Sub

		Protected Sub btnSkip_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			'Response.Redirect( Constants.WELCOME_MSG + "?e=" + Constants.NEW_USER_REGISTERED + "&" + Constants.REGISTRATION_PARAM + "=" + Constants.REGISTRATION_VALUE );
			Response.Redirect(Constants.WELCOME)
		End Sub

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable uploadImageForm.
		====================
		Public ReadOnly Property uploadImageForm() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated_uploadImageForm
			End Get
		End Property
		'set { Migrated_uploadImageForm = value; }
	End Class
End Namespace






